
const {
    connect,
    add,
    update,
    getOne,
    getList,
    getCount,
}=require('../common/db.js')



const { Parser } = require('sql-ddl-to-json-schema');
const parser = new Parser('mysql');
const lodash=require('lodash')
function ddl2json(ddlStr) {
    const jsonSchemaDocuments = parser.feed(ddlStr)
        .toJsonSchemaArray({});
    var aobj={}
    lodash.map(jsonSchemaDocuments[0].definitions,(item,key)=>{
        aobj[key]=""
    })

    return  aobj;
}

async function getDDLByTableName(tableName) {
    var db=connect({
        host:process.env.host,
        user:process.env.user,
        password:process.env.password,
        database:process.env.database,
    })

    var [rows] = await db.raw(`show create table ${tableName}`)
    return rows[0]['Create Table'];
}



module.exports = {
    ddl2json,
    getDDLByTableName,
}

